const { readFile, writeFile } = require('fs/promises')

const FILE_PATH = '../entry/src/main/ets/tests/Suites.ets'

function countDisabledTests(content) {
  const regex = /disabledTests\s*:\s*\[\s*([^\]]+?)\s*\]/gs
  let count = 0
  let match
  while ((match = regex.exec(content)) !== null) {
    const disabledTestsContent = match[1]
    const disabledTests = disabledTestsContent
      .split(',')
      .map(test => test.trim())
      .filter(test => test.length > 0)
    count += disabledTests.length
  }
  return count
}

async function removeDisabledTests() {
  try {
    let content = await readFile(FILE_PATH, 'utf8')
    const totalDisabledTests = countDisabledTests(content)
    const updatedContent = content.replace(/\s*\]\s*,\s*\r?\n\s*disabledTests\s*:\s*\[/g, '')
    await writeFile(FILE_PATH, updatedContent, 'utf8')
    console.info('File updated successfully.')
    console.info(`Total tests restored: ${totalDisabledTests}`)
  } catch (err) {
    console.error('Error processing file:', err)
  }
}

removeDisabledTests()